package edu.chl.dat255.bluebanana.presentation.impl;

import edu.chl.dat255.bluebanana.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.SeekBar;
import edu.chl.dat255.bluebanana.data.impl.DbConst;
import edu.chl.dat255.bluebanana.presentation.ISoundProfileSettings;
import edu.chl.dat255.bluebanana.utils.DatabaseUtil;

public class SoundProfileSettings extends Activity implements ISoundProfileSettings {
	private SeekBar volumeSeekBar;
	private SeekBar notificationSeekBar;
	private Button changeSoundButton;
	private CheckBox muteButton;
	private CheckBox vibrateButton;
	private AudioManager audioManager;
	private int ringMaxValue;
	private int notificationMaxValue;
	private long profileId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.soundsettings);
		
		profileId = getIntent().getLongExtra("Profile", -1);
		
		audioManager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
		ringMaxValue = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
		notificationMaxValue = audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
	
		volumeSeekBar = (SeekBar) findViewById(R.id.volumeSeekBar);
		volumeSeekBar.setMax(ringMaxValue);
		volumeSeekBar.setKeyProgressIncrement(1); 
		
		notificationSeekBar = (SeekBar) findViewById(R.id.notificationSeekBar);
		notificationSeekBar.setMax(notificationMaxValue);
		notificationSeekBar.setKeyProgressIncrement(1);
		
		changeSoundButton = (Button) findViewById(R.id.changeTone);
		changeSoundButton.setOnClickListener(this);
		
		muteButton = (CheckBox) findViewById(R.id.muteButton);
		vibrateButton = (CheckBox) findViewById(R.id.vibrateButton);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		volumeSeekBar.setProgress(DatabaseUtil.getIntValue(profileId, DbConst.KEY_CALL_VOLUME, this));
		notificationSeekBar.setProgress(DatabaseUtil.getIntValue(profileId, DbConst.KEY_NOTIFICATION_VOLUME, this));
		muteButton.setChecked(DatabaseUtil.getBooleanValue(profileId, DbConst.KEY_MUTE_ENABLE, this));
		vibrateButton.setChecked(DatabaseUtil.getBooleanValue(profileId, DbConst.KEY_VIBRATE_ENABLE, this));
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		DatabaseUtil.sendValueToDatabase(profileId, DbConst.KEY_MUTE_ENABLE, muteButton.isChecked(), SoundProfileSettings.this);
		DatabaseUtil.sendValueToDatabase(profileId, DbConst.KEY_VIBRATE_ENABLE, vibrateButton.isChecked(), SoundProfileSettings.this);
		DatabaseUtil.sendValueToDatabase(profileId, DbConst.KEY_CALL_VOLUME, volumeSeekBar.getProgress(), SoundProfileSettings.this);
		DatabaseUtil.sendValueToDatabase(profileId, DbConst.KEY_NOTIFICATION_VOLUME, notificationSeekBar.getProgress(), SoundProfileSettings.this);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(changeSoundButton)) {
			Intent intent = new Intent(SoundProfileSettings.this, Preferences.class);
			intent.putExtra("Profile", profileId);
			startActivity(intent);
		}
	}
}
